//
//  BMSLinkOpener.m
//  WDGroMore
//
//  Created by YeJary on 2025/5/23.
//

// BMSLinkOpener.m
#import "BMSLinkOpener.h"
#import "BMSInitializer.h"
#import <SafariServices/SafariServices.h>

@implementation BMSLinkOpener

+ (void)openURL:(NSString *)urlString {
    if (urlString.length == 0) return;
    
    NSURL *url = [NSURL URLWithString:urlString];
    if (!url || !url.scheme) return;

    dispatch_async(dispatch_get_main_queue(), ^{
        UIViewController *topVC = [self topViewController];
        if ([url.scheme hasPrefix:@"http"]) {
            SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url];
            safariVC.modalPresentationStyle = UIModalPresentationPageSheet;
            [topVC presentViewController:safariVC animated:YES completion:nil];
        } else if ([url isFileURL]) {
            // 打开本地 html 文件
            UIViewController *vc = [self.class viewControllerForView:self];
            UIWebView *webView = [[UIWebView alloc] initWithFrame:vc.view.bounds];
            webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [vc.view addSubview:webView];
            [webView loadRequest:[NSURLRequest requestWithURL:url]];
            vc.title = @"详情";
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            [topVC presentViewController:nav animated:YES completion:nil];
        }
    });
}

+ (void)openTerms {
    [self openURL:[BMSInitializer shared].launchModel.termUrl];
}

+ (void)openPrivacy {
    [self openURL:[BMSInitializer shared].launchModel.privacyUrl];
}

+ (UIViewController *)topViewController {
    UIWindow *keyWindow = nil;

    // iOS 13+ 获取活跃窗口
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene *scene in UIApplication.sharedApplication.connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive) {
                keyWindow = scene.windows.firstObject;
                break;
            }
        }
    }

    if (!keyWindow) {
        keyWindow = UIApplication.sharedApplication.keyWindow ?: UIApplication.sharedApplication.windows.firstObject;
    }

    UIViewController *rootVC = keyWindow.rootViewController;
    return [self findTopViewControllerFrom:rootVC];
}

+ (UIViewController *)findTopViewControllerFrom:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self findTopViewControllerFrom:((UINavigationController *)vc).visibleViewController];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self findTopViewControllerFrom:((UITabBarController *)vc).selectedViewController];
    } else if (vc.presentedViewController) {
        return [self findTopViewControllerFrom:vc.presentedViewController];
    } else {
        return vc;
    }
}

+ (UIViewController *)viewControllerForView:(UIView *)view {
    UIResponder *responder = view;
    while (responder && ![responder isKindOfClass:[UIViewController class]]) {
        responder = responder.nextResponder;
    }
    return (UIViewController *)responder;
}

@end
